package com.freedom.admin.infrastructure.persistence.repository;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.freedom.admin.domain.repository.SysUserRepository;
import com.freedom.admin.infrastructure.persistence.assembler.SysUserAssembler;
import com.freedom.admin.infrastructure.persistence.mapper.SysUserMapper;
import com.freedom.admin.infrastructure.persistence.pojo.bo.UserAuthBO;
import com.freedom.admin.infrastructure.persistence.pojo.bo.UserBO;
import com.freedom.admin.infrastructure.persistence.pojo.bo.UserFormBO;
import com.freedom.admin.infrastructure.persistence.pojo.po.SysUserPO;
import com.freedom.admin.infrastructure.persistence.pojo.query.UserPageQuery;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@AllArgsConstructor
public class SysUserRepositoryImpl implements SysUserRepository {

    private final SysUserMapper sysUserMapper;

    private final SysUserAssembler sysUserAssembler;

    @Override
    public Integer save(UserBO userBO) {
        SysUserPO sysUserPO = sysUserAssembler.toPO(userBO);
        sysUserMapper.insert(sysUserPO);
        return sysUserPO.getId();
    }

    @Override
    public void modifyById(UserBO userBO) {
        SysUserPO sysUserPO = sysUserAssembler.toPO(userBO);
        sysUserMapper.updateById(sysUserPO);
    }

    @Override
    public void removeByIds(List<Integer> ids) {
        sysUserMapper.deleteByIds(ids);
    }

    @Override
    public void modifyUser(UserBO userBO) {
        SysUserPO sysUserPO = sysUserAssembler.toPO(userBO);
        sysUserMapper.updateById(sysUserPO);
    }

    @Override
    public UserAuthBO getAuthInfoByUsername(String username) {
        return sysUserMapper.getAuthInfoByUsername(username);
    }

    @Override
    public UserBO findById(Integer userId) {
        SysUserPO sysUserPO = sysUserMapper.selectById(userId);
        return sysUserAssembler.toBO(sysUserPO);
    }

    @Override
    public Page<UserBO> listUserPages(UserPageQuery queryParams) {
        Page<UserBO> page = new Page<>(queryParams.getCurrent(), queryParams.getPageSize());
        return sysUserMapper.listUserPages(page, queryParams);
    }

    @Override
    public UserFormBO findUserFormData(Integer userId) {
        return sysUserMapper.getUserFormData(userId);
    }
}
